<?php

defined('_JEXEC') or die;
require_once (JPATH_COMPONENT.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'utils.php');
class BakerysModelItem extends JModelAdmin
{

	protected $text_prefix = 'COM_BAKERYS';

	protected function canDelete($record)
	{
		if (!empty($record->id))
		{
			if ($record->state != -2)
			{
				return;
			}
			return parent::canDelete($record);
		}
	}

	public function getTable($type = 'Item', $prefix = 'BakerysTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	public function getForm($data = array(), $loadData = true)
	{
		$app = JFactory::getApplication();

		// Get the form.
		$form = $this->loadForm('com_bakerys.item', 'item', array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form))
		{
			return false;
		}
		
		// Determine correct permissions to check.
		if ($this->getState('item.id'))
		{
			// Existing record. Can only edit in selected categories.
			$form->setFieldAttribute('catid', 'action', 'core.edit');
		}
		else
		{
			// New record. Can only create in selected categories.
			$form->setFieldAttribute('catid', 'action', 'core.create');
		}

		// Modify the form based on access controls.
		if (!$this->canEditState((object) $data))
		{
			// Disable fields for display.
			$form->setFieldAttribute('ordering', 'disabled', 'true');
			$form->setFieldAttribute('state', 'disabled', 'true');

			// Disable fields while saving.
			// The controller has already verified this is a record you can edit.
			$form->setFieldAttribute('ordering', 'filter', 'unset');
			$form->setFieldAttribute('state', 'filter', 'unset');
		}

		return $form;
	}

	/**
	 * Method to get the data that should be injected in the form.
	 *
	 * @return  mixed  The data for the form.
	 * @since   1.6
	 */
	protected function loadFormData()
	{
		// Check the session for previously entered form data.
		$data = JFactory::getApplication()->getUserState('com_bakerys.edit.item.data', array());

		if (empty($data))
		{
			//$data = $this->getItem();
			$data = parent::getItem();
			// Prime some default values.
			if ($this->getState('item.id') == 0)
			{
				$app = JFactory::getApplication();
				$data->set('catid', $app->input->get('catid', $app->getUserState('com_bakerys.items.filter.category_id'), 'int'));
			}
		}

		return $data;
	}

	
	protected function getReorderConditions($table)
	{
		$condition = array();
		$condition[] = 'catid = '.(int) $table->catid;
		return $condition;
	}
	
	/**
	 * Prepare and sanitise the table prior to saving.
	 *
	 * @since   1.6
	 */
	protected function prepareTable($table)
	{
		$date = JFactory::getDate();
		$user = JFactory::getUser();


		if (empty($table->id))
		{
			// Set ordering to the last item if not set
			if (empty($table->ordering))
			{
				$db = JFactory::getDbo();
				$db->setQuery('SELECT MAX(ordering) FROM #__bakery_items');
				$max = $db->loadResult();

				$table->ordering = $max + 1;
			}
		}
	}
	
	public function getItem($pk = null)
	{				
		$db		= $this->getDbo();
		$query	= $db->getQuery(true);
		$id = JRequest::getVar("id");
		$user	= JFactory::getUser();				
		$isAdmin = $user->get("isRoot");
		if($isAdmin)
		{
			$query->select("a.*, a.price as location_price");
			$query->from("#__bakery_items a");
			$query->where('a.id = '.(int) $id);
		}
		else 
		{			
			$location_id= utils::GetStoreID();
			$query->select("a.*, i.price as location_price");
			$query->from("#__bakery_items a");
			$query->join('LEFT', '#__bakery_location_items AS i ON i.item_id=a.id and i.location_id=' .$location_id);
			$query->where('a.id = '.(int) $id);			
		}		

		$db->setQuery($query);	
		return $db->loadObject();
		
	}
	
}
